<?php

namespace EasyVote\EasyVoteBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use EasyVote\EasyVoteBundle\Entity\Search;

class SearchController extends Controller
{
    /**
     * @Template
     * Show user form
     * @param Request $request
     * @return Response The form to search for a user. If the form has been
     * submitted, it returns the query results.
     */
    public function showAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
		
		// Create the form 
		// Search by euid, displayName, classification, degree
        $form = $this->createFormBuilder()
            ->add('euid', 'text', array(
					'required' => false
				)
			)
            ->add('displayName', 'text', array(
					'required' => false, 
					'trim' => false
				)
			)
			->add('classification', 'entity', array(
					'class' => 'EasyVoteBundle:Classification', 
					'property' => 'name', 
					'empty_value' => 'Choose an option', 
					'required' => false
				)
			)
			->add('degrees', 'entity', array(
					'class' => 'EasyVoteBundle:Degree', 
					'property' => 'displayName' , 
					'empty_value' => 'Choose an option', 
					'required' => false
				)
			)
			/*
		    ->add('departments', 'entity', array(
					'class' => 'EasyVoteBundle:Department', 
					'property' => 'name', 
					'empty_value' => 'Choose an option', 
					'multiple' => 'false', 'required' => false
				)
			)
			*/
            ->add('submit', 'submit', array('label' => 'Search'))
            ->getForm();

        //this block has "what to do if form is being submitted"
        if ($request->getMethod() == 'POST') {
            $form->handleRequest($request);
            if ($form->isValid()) {
				$data = $form->getData();
				
				// Create an SQL Query to search through the database
				$qb = $em->createQueryBuilder();
				
				// Need to user where('1=1') so we can use the andWhere later
				$qb = $qb->select('u')->from('EasyVoteBundle:User', 'u')
				->where('1 = 1');
				
				// Add the euid to the query
				if ($data['euid'] != null) {					
					$data['euid'] = trim($data['euid']);					
					if (!empty($data['euid'])) {
						$qb = $qb->andWhere('u.euid = :euid')
						->setParameter('euid', $data['euid']);
					}
				}
				
				// Add the display name to the query
				if ($data['displayName'] != null) {					
					$data['displayName'] = trim($data['displayName']);					
					if (!empty($data['displayName'])) {
						$qb = $qb->andWhere('u.displayName = :displayName')
						->setParameter('displayName', $data['displayName']);
					}
				}
				
				// Add the classification to the query
				if ($data['classification'] != null) {					
					if (!empty($data['classification'])) {
						$classificationIDs = array();
						$classifications = $data['classification'];
						foreach ($classifications as $classification) {
							$classificationIDs[] = $classification.getID();
						}
						
						$qb = $qb->join('u.classification', 'c')
						->andWhere('c.id IN (:classification)')
						->setParameter('classification', $classificationIDs);
					}
				}
				
				// Add the degrees to the query
				if ($data['degrees'] != null) {					
					if (!empty($data['degrees'])) {
						$degreesIDs = array();
						$degrees = $data['degrees'];
						foreach ($degrees as $degree) {
							$degreesIDs[] = $degree.getID();
						}
						
						$qb = $qb->join('u.degrees', 'd')
						->andWhere('d.id IN (:degree)')
						->setParameter('degree', $degrees);
					}
				}
				
				// Execute the query
				$queryResults = $qb->getQuery()->getResult();

				// Return the results of the query
				return array ('query_results' => $queryResults);
            }
        }
        
        // Return the form to show
        return array('isAdmin' => $this->getUser()->isAdmin(), 'form' => $form->createView());
    }
}

?>
